package com.xtianzhuang.www.controller.finance;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VUserAccount;
import com.xtianzhuang.www.entity.VUserDailyBalance;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteBalanceRequest;
import com.xtianzhuang.www.entity.request.QueryDailyBalanceRequest;
import com.xtianzhuang.www.entity.request.QueryDailyBalanceStatisticsRequest;
import com.xtianzhuang.www.entity.request.SaveAccountRequest;
import com.xtianzhuang.www.entity.request.SaveDailyBalanceRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.DailyBalance;
import com.xtianzhuang.www.entity.response.DeleteBalanceResponse;
import com.xtianzhuang.www.entity.response.QueryDailyBalanceResponse;
import com.xtianzhuang.www.entity.response.QueryDailyBalanceStatisticsResponse;
import com.xtianzhuang.www.entity.response.SaveAccountResponse;
import com.xtianzhuang.www.entity.response.SaveDailyBalanceResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/finance")
public class FinanceController extends BaseController {
	@RequestMapping(value = "/addAccount")
	public ModelAndView addAccount(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/account-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/assets")
	public ModelAndView assets(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/assets");
		// 债券账户
		List<VUserAccount> bondAccountList = financeServiceBiz.getBondAccount();
		BigDecimal bondAssets = new BigDecimal(0);
		for (VUserAccount vUserAccount : bondAccountList) {
			bondAssets = bondAssets.add(vUserAccount.getAssets());
		}
		// 负债账户
		List<VUserAccount> liabilityAccountList = financeServiceBiz.getliabilityAccounts();
		BigDecimal liabilityAssets = new BigDecimal(0);
		for (VUserAccount vUserAccount : liabilityAccountList) {
			liabilityAssets = liabilityAssets.add(vUserAccount.getAssets());
		}
		BigDecimal assets = bondAssets.subtract(liabilityAssets);
		// 5万目标
		BigDecimal goal1 = assets.multiply(new BigDecimal(100)).divide(new BigDecimal(50000));
		if (goal1.compareTo(new BigDecimal(100)) > 0) {
			goal1 = new BigDecimal(100);
		}
		// 10万目标
		BigDecimal goal2 = assets.multiply(new BigDecimal(100)).divide(new BigDecimal(100000));
		if (goal2.compareTo(new BigDecimal(100)) > 0) {
			goal2 = new BigDecimal(100);
		}
		modelAndView.addObject("assets", assets.toString());
		modelAndView.addObject("bondAssets", bondAssets.toString());
		modelAndView.addObject("liabilityAssets", liabilityAssets.toString());
		modelAndView.addObject("bondAccountList", bondAccountList);
		modelAndView.addObject("liabilityAccountList", liabilityAccountList);
		modelAndView.addObject("goal1", goal1.toString());
		modelAndView.addObject("goal2", goal2.toString());
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/addDailyBalancePay")
	public ModelAndView addDailyBalancePay(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/daily-balance-pay-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/addDailyBalanceTransfer")
	public ModelAndView addDailyBalanceTransfer(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/daily-balance-transfer-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/dailyBalanceStatistics")
	@Log
	public ModelAndView dailyBalanceStatistics(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("finance/daily-balance-statistics");
		List<DailyBalance> dailyBalanceList = financeServiceBiz.getDailyBalanceByMonth();
		List<DailyBalance> dailyIncomeList = financeServiceBiz.getDailyIncomeByMonth();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.addObject("dailyBalanceList", dailyBalanceList);
		modelAndView.addObject("dailyIncomeList", dailyIncomeList);
		String pay = financeServiceBiz.getDailyBalanceTotalByMonth("", "1");
		String income = financeServiceBiz.getDailyBalanceTotalByMonth("", "2");
		modelAndView.addObject("pay", pay);
		modelAndView.addObject("income", income);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxQueryDailyBalanceStatistics")
	@ResponseBody
	@Log
	public QueryDailyBalanceStatisticsResponse queryDailyBalanceStatistics(QueryDailyBalanceStatisticsRequest request) {
		QueryDailyBalanceStatisticsResponse response = new QueryDailyBalanceStatisticsResponse();
		List<DailyBalance> dailyPayList = financeServiceBiz.getDailyBalanceByMonth(request.getDate());
		List<DailyBalance> dailyIncomeList = financeServiceBiz.getDailyIncomeByMonth(request.getDate());
		String pay = financeServiceBiz.getDailyBalanceTotalByMonth(request.getDate(), "1");
		String income = financeServiceBiz.getDailyBalanceTotalByMonth(request.getDate(), "2");
		response.setIncomeList(dailyIncomeList);
		response.setPayList(dailyPayList);
		response.setPay(pay);
		response.setIncome(income);
		response.setCode(CodeConstants.SUCCESS);
		response.setMessage("查询成功");
		return response;
	}

	@RequestMapping(value = "/queryDailyBalance")
	public ModelAndView queryDailyBalance(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/daily-balance-list");
		// 跳回原地址
		return modelAndView;
	}


	@RequestMapping("/ajaxDeleteBalance")
	@ResponseBody
	@Log(type = LogType.FINANCE)
	public DeleteBalanceResponse deleteBalance(DeleteBalanceRequest request) {
		DeleteBalanceResponse response = new DeleteBalanceResponse();
		try {
			financeServiceBiz.deleteDailyBalance(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage("删除失败");
		}
		return response;
	}

	@RequestMapping(value = "/addDailyBalanceIncome")
	public ModelAndView addDailyBalanceIncome(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		modelAndView.setViewName("finance/daily-balance-income-add");
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/ajaxSaveDailyBalancePay")
	@ResponseBody
	public SaveDailyBalanceResponse ajaxSaveDailyBalancePay(SaveDailyBalanceRequest request) {
		SaveDailyBalanceResponse response = new SaveDailyBalanceResponse();
		try {
			financeServiceBiz.saveUserDailyBalancePay(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/ajaxSaveDailyBalanceTransfer")
	@ResponseBody
	public SaveDailyBalanceResponse ajaxSaveDailyBalanceTransfer(SaveDailyBalanceRequest request) {
		SaveDailyBalanceResponse response = new SaveDailyBalanceResponse();
		try {
			financeServiceBiz.saveUserDailyBalanceTransfer(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/ajaxSaveDailyBalanceIncome")
	@ResponseBody
	public SaveDailyBalanceResponse ajaxSaveDailyBalanceIncome(SaveDailyBalanceRequest request) {
		SaveDailyBalanceResponse response = new SaveDailyBalanceResponse();
		try {
			financeServiceBiz.saveUserDailyBalanceIncome(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQueryDailyBalance")
	@ResponseBody
	@Log(type = LogType.FINANCE)
	public QueryDailyBalanceResponse queryDailyBalance(QueryDailyBalanceRequest request) {
		QueryDailyBalanceResponse response = new QueryDailyBalanceResponse();
		try {
			long count = financeServiceBiz.queryDailyBalanceCount(request);
			List<VUserDailyBalance> list = financeServiceBiz.queryDailyBalance(request);
			response.setData(list);
			response.setTotal(count);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping(value = "/ajaxSaveAccount")
	@ResponseBody
	public SaveAccountResponse ajaxSaveAccount(SaveAccountRequest request) {
		SaveAccountResponse response = new SaveAccountResponse();
		try {
			financeServiceBiz.saveAccount(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}


}
